//
//  HMTabBarController.swift
//  webo
//
//  Created by apple on 2016/11/12.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class HMTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let hmtabbar = HMTabBar()
        
        setValue(hmtabbar, forKey: "tabBar")
        hmtabbar.composeColsure = {[weak self] in
            
        let composeView:HMComposeView = HMComposeView()
        self?.view.addSubview(composeView)
        composeView.frame = (self?.view.bounds)!
        composeView.show(fromvc: self!)
            
        }
        addChildViewcontrollers()

//    tabbar_home_selected
    }

    func addChildViewcontrollers(){
        addChildViewcontrollers(vc: HMHomdController(), title: "主页", imgName: "tabbar_home",index: 0)
        addChildViewcontrollers(vc: HMMessageViewController(), title: "消息", imgName: "tabbar_message_center",index: 1)
        addChildViewcontrollers(vc: HMDiscoverViewController(), title: "发现", imgName: "tabbar_discover",index: 2)
        addChildViewcontrollers(vc: HMProfleViewController(), title: "我", imgName: "tabbar_profile",index: 3)
        
    }
    
    private func addChildViewcontrollers(vc: UIViewController, title: String, imgName: String,index: NSInteger){
        vc.title = title
        vc.tabBarItem.title = title
        vc.tabBarItem.tag = index
        vc.tabBarItem.image = UIImage(named: imgName)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named: "\(imgName)"+"_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: .selected)
        
        let navi = HMNaviViewController(rootViewController: vc)
        self.addChildViewController(navi)
    }

    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
//        print(item.tag)
        var index = 0
        
        for subview in tabBar.subviews {
            if subview.isKind(of: NSClassFromString("UITabBarButton")!) {
                if index == item.tag {
                 
                    for v in subview.subviews {
                        if v.isKind(of: NSClassFromString("UIImageView")!) {
//                            print("ok")
                            v.transform = CGAffineTransform(scaleX: 0.7, y: 0.7)
                            UIView.animate(withDuration: 0.25, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 6, options: [], animations: {
                              v.transform = CGAffineTransform.identity
                            }, completion: { (make) in
//                                print("")
                            })
                            
                        }
                    }
                }
                index += 1
            }
        }
    }
    
    

}
